local kl__yisuan = fk.CreateSkill {
  name = "kl__yisuan",
}

Fk:loadTranslationTable{
  ["kl__yisuan"] = "亦算",
  [":kl__yisuan"] = "出牌阶段限一次，当你使用的基本牌及普通锦囊牌结算后或你装备区内的牌不因弃置而进入弃牌堆后，你可以减1点体力上限，摸一张牌并获得之。",

  ["#kl__yisuan-invoke"] = "亦算：是否减1点体力上限，摸一张牌并获得%arg？",

  ["$kl__yisuan1"] = "凶狼善谋，贪豺可算！",
  ["$kl__yisuan2"] = "虓虎虽勇，亦在吾算计之中！",
}

kl__yisuan:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(kl__yisuan.name) and player.phase == Player.Play and
    (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) and player.room:getCardArea(data.card) == Card.Processing and
    player:usedSkillTimes(kl__yisuan.name, Player.HistoryPhase) == 0
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = kl__yisuan.name,
      prompt = "#kl__yisuan-invoke:::"..data.card:toLogString(),
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, -1)
    if player.dead then return end
    player:drawCards(1, kl__yisuan.name)
    room:obtainCard(player, data.card, true, fk.ReasonJustMove, player, kl__yisuan.name)
  end,
})

kl__yisuan:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(kl__yisuan.name) and player.phase == Player.Play and player:usedSkillTimes(kl__yisuan.name, Player.HistoryPhase) == 0 then
      for _, move in ipairs(data) do
          if move.from and move.from == player and move.toArea == Card.DiscardPile then
              for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.PlayerEquip and move.moveReason ~= fk.ReasonDiscard then
                  event:setCostData(self, {card = info.cardId})
                  return true
                end
              end
          end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local card = Fk:getCardById(event:getCostData(self).card)
    return player.room:askToSkillInvoke(player, {
      skill_name = kl__yisuan.name,
      prompt = "#kl__yisuan-invoke:::"..card:toLogString(),
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, -1)
    if player.dead then return end
    player:drawCards(1, kl__yisuan.name)
    room:obtainCard(player, event:getCostData(self).card, true, fk.ReasonJustMove, player, kl__yisuan.name)
  end,
})


return kl__yisuan
